package service

import (
	"dao"
	"pojo/form"
	"pojo/vo"
	"constant"
	"pojo/model"
)

type AdvService struct {
	dao *dao.AdvDao
}

// 构造广告服务层
func NewAdvService(dao *dao.AdvDao) *AdvService {
	return &AdvService{
		dao: dao,
	}
}

// 添加广告信息
func (this *AdvService) InsertAdv(advInfo *form.AdvForm) error {

	adv := &model.Adv{
		Img: advInfo.Img,
		Url: advInfo.Url,
	}

	_, err := this.dao.InsertAdv(adv)

	return err
}

// 分页查询广告
func (this *AdvService) QueryAdvs(queryForm *form.AdvPageQueryForm) *vo.TableVo {

	result := &vo.TableVo{}
	result.Code = constant.SUCCESS.Code
	result.Msg = constant.SUCCESS.Msg

	size, _ := this.dao.Count(queryForm)
	result.Total = size

	if size > 0 {
		news, _ := this.dao.List(queryForm)
		result.Items = news
	}

	return result

}

// 根据ID删除广告
func (this *AdvService) DeleteAdv(id uint64) error {

	return this.dao.Delete(id)
}

// 根据id查询广告
func (this *AdvService) FindAdv(id uint64) (*model.Adv, error) {

	return this.dao.FindAdv(id)
}

// 根据id修改广告信息
func (this *AdvService) UpdateAdv(advInfo *form.AdvForm) error {

	adv := &model.Adv{
		Img: advInfo.Img,
		Url: advInfo.Url,
	}
	adv.ID = uint(advInfo.Id)

	return this.dao.UpdateAdv(adv)
}
